#垃圾网站一样被我搞定

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from lxml import etree
import time

class Lagou(object):
    exepath=r'E:\programlibs\chromedriver\chromedriver.exe'
    def __init__(self):
        self.driver=webdriver.Chrome(executable_path=self.exepath)
        self.url='https://www.lagou.com/zhaopin/qukuailian/?labelWords=label'

    def run(self):
        self.driver.get(self.url)
        while True:
            self.parse_url(self.driver.page_source)
            time.sleep(2)
            next=WebDriverWait(driver=self.driver,timeout=10).until(
                EC.presence_of_element_located((By.XPATH,'//div[@class="pager_container"]/a[last()]'))
            )
            #next=self.driver.find_element_by_xpath('//div[@class="pager_container"]/a[last()]')
            if "page_no pager_next_disabled" in next.get_attribute("class"):
                break
            next.click()


    def parse_url(self,page_source):
        html=etree.HTML(page_source)
        lis=html.xpath('//li[@class="con_list_item default_list"]')
        for li in lis:
            link=li.xpath('.//a[@class="position_link"]/@href')[0]
            self.parse_detail_url(link)
            time.sleep(2)

    def parse_detail_url(self,link):
        self.driver.execute_script('window.open("%s")'%link)
        self.driver.switch_to.window(self.driver.window_handles[1])
        self.driver.get(link) #这一句代码只是为了让浏览器切换到页面之后,driver也切换到那个页面
        html=etree.HTML(self.driver.page_source)
        next = WebDriverWait(driver=self.driver, timeout=10).until(
            EC.presence_of_element_located((By.XPATH, '//div[@class="position-head"]'))
        )
        position=html.xpath('//div[@class="position-head"]//h2[@class="name"]/text()')[0]
        company=html.xpath('//div[@class="position-head"]//h4[@class="company"]/text()')[0]
        salary=html.xpath('//div[@class="position-head"]//span[@class="salary"]/text()')[0]
        position_dict={
            'position':position,
            'company':company,
            'salary':salary
        }
        print(position_dict)
        print('*'*30)
        self.driver.close()
        self.driver.switch_to.window(self.driver.window_handles[0])
        time.sleep(2)




if __name__=='__main__':
    lagou=Lagou()
    lagou.run()
